package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ21_字符串解密 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/21.txt"));
        String s1 = in.nextLine();
        String s2 = in.nextLine();
        Set<Character> set2 = new HashSet<>();
        for (char a : s2.toCharArray()) {
            set2.add(a);
        }
        String hash = "0123456789abcdef";
        int index = 0;
        List<String> sArray = new ArrayList<>();
        for (int i = 0; i < s1.length(); i++) {
            if (hash.contains(s1.charAt(i) + "")) {
                sArray.add(s1.substring(index, i));
                index = i + 1;
            } else if (i == s1.length() - 1) {
                sArray.add(s1.substring(index, i + 1));
            }
        }
        String now = "";
        int nowCnt = 0;
        for (String s : sArray) {
            if ("".equals(s)) {
                continue;
            }
            Set<Character> set = new HashSet<>();
            for (char a : s.toCharArray()) {
                set.add(a);
            }
            int len = set.size();
            if (len < nowCnt) {
                continue;
            }
            if (len <= set2.size()) {
                if (len > nowCnt) {
                    now = s;
                    nowCnt = len;
                } else if (s.compareTo(now) > 0) {
                    now = s;
                }
            }
        }
        System.out.println(now);
    }
}
